﻿using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Linq;
using HiStaff.iSystem;
using HiStaff.Domain;
using System;
using System.ComponentModel;

namespace HiStaff.Control
{
    [ToolboxBitmap(typeof(ComboBox))]
    public class HiComboBox : Janus.Windows.EditControls.UIComboBox
    {
        bool _isRequire = false;
        bool _addFirstNullRow = true;
        bool _bindDataSourceOnLoad = true;
        private bool _readOnlyWhenEdit = false;
        private bool _searchControl = false;
        public bool SearchControl
        {
            get { return _searchControl; }
            set { _searchControl = value; }
        }
        public bool ReadOnlyWhenEdit
        {
            get { return _readOnlyWhenEdit; }
            set { _readOnlyWhenEdit = value; }
        }
        [DefaultValue(false)]
        public bool IsRequire
        {
            get
            {
                return _isRequire;
            }
            set
            {
                _isRequire = value;
                if (value)
                    this.BackColor = HiStaff.iSystem.Data.COLOR_REQUIREMENT;
                else this.BackColor = HiStaff.iSystem.Data.COLOR_NONREQUIREMENT;
            }
        }
        public string DataField { get; set; }
        public string NeedDataList { get; set; }
        public string NeedDataFilter { get; set; }
        public HiComboBox ControlBindOnIndexChanged { get; set; }
        public bool AddFirstNullRow { get { return _addFirstNullRow; } set { _addFirstNullRow = value; } }
        public bool BindDataSourceOnLoad { get { return _bindDataSourceOnLoad; } set { _bindDataSourceOnLoad = value; } }
        protected override void OnCreateControl()
        {
            try
            {
                base.OnCreateControl();
                if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime && _bindDataSourceOnLoad)
                {
                    ReBind();
                }
            }
            catch
            {
                return;
            }
        }
        public HiComboBox()
        {
            this.VisualStyle = Janus.Windows.UI.VisualStyle.Office2010;
            this.AutoComplete = false;
            this.MaxDropDownItems = 20;
            this.Text = null;
        }
        public void ReBind()
        {
            if (!string.IsNullOrEmpty(this.NeedDataList))
            {
                var chkList = (from o in HiStaff.iSystem.Data.LISTTABLE
                               where o.TABLE_NAME.ToUpper() == this.NeedDataList.ToUpper()
                               select o).Count();
                if (chkList > 0)
                {
                    if (string.IsNullOrEmpty(this.DisplayMember))
                        this.DisplayMember = HiStaff.iSystem.Data.COL_NAME;
                    if (string.IsNullOrEmpty(this.ValueMember))
                        this.ValueMember = HiStaff.iSystem.Data.COL_ID;
                    //DataTable data = OracleHelper.ExcuteSelectDataTable(StaticObject.PREFIX_PROC_SELECT_LIST + this.NeedDataList);
                    DataTable data = HiStaff.iSystem.Data.DATALIST.Tables[this.NeedDataList];
                    DataTable dSource;
                    if (string.IsNullOrEmpty(this.NeedDataFilter))
                    {
                        dSource = data.Copy();
                    }
                    else
                    {
                        //DataView dv = data.DefaultView;
                        //dv.RowFilter = this.NeedDataFilter;
                        //dSource = dv.Table;

                        dSource = data.Clone();
                        DataRow[] dr = data.Select(this.NeedDataFilter);
                        foreach (DataRow idr in dr)
                        {
                            dSource.ImportRow(idr);
                        }
                    }
                    if (this.AddFirstNullRow)
                        dSource.Rows.InsertAt(dSource.NewRow(), 0);
                    this.DataSource = dSource;
                    this.SelectedIndex = 0;
                }
                else
                {
                    var chkTable = (from o in HiStaff.iSystem.Data.ALLTABLE
                                    where o.TABLE_NAME.ToUpper() == this.NeedDataList.ToUpper()
                                    select o).Count();
                    if (string.IsNullOrEmpty(this.DisplayMember))
                        this.DisplayMember = HiStaff.iSystem.Data.COL_NAME;
                    if (string.IsNullOrEmpty(this.ValueMember))
                        this.ValueMember = HiStaff.iSystem.Data.COL_ID;
                    DataTable data = Service.ExcuteStoreToDataTable(HiStaff.iSystem.Data.PREFIX_PROC_SELECT + this.NeedDataList);
                    DataTable dSource;
                    if (string.IsNullOrEmpty(this.NeedDataFilter))
                    {
                        dSource = data;
                    }
                    else
                    {
                        //DataView dv = data.DefaultView;
                        //dv.RowFilter = this.NeedDataFilter;
                        //dSource = dv.Table;
                        dSource = data.Clone();
                        DataRow[] dr = data.Select(this.NeedDataFilter);
                        foreach (DataRow idr in dr)
                        {
                            dSource.ImportRow(idr);
                        }
                    }
                    if (this.AddFirstNullRow)
                        dSource.Rows.InsertAt(dSource.NewRow(), 0);
                    this.DataSource = dSource;
                    this.SelectedIndex = 0;
                }
            }
        }

        protected override void OnSelectedIndexChanged(System.EventArgs e)
        {
            try
            {
                base.OnSelectedIndexChanged(e);
                if (System.ComponentModel.LicenseManager.CurrentContext.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
                {
                    if (ControlBindOnIndexChanged != null)
                    {
                        if (this.SelectedValue != null)
                        {
                            if (!string.IsNullOrEmpty(ControlBindOnIndexChanged.NeedDataList))
                            {
                                var chkList = (from o in HiStaff.iSystem.Data.LISTTABLE
                                               where o.TABLE_NAME.ToUpper() == ControlBindOnIndexChanged.NeedDataList.ToUpper()
                                               select o).Count();
                                if (chkList > 0)
                                {
                                    if (string.IsNullOrEmpty(ControlBindOnIndexChanged.DataMember))
                                        ControlBindOnIndexChanged.DisplayMember = HiStaff.iSystem.Data.COL_NAME;
                                    if (string.IsNullOrEmpty(ControlBindOnIndexChanged.ValueMember))
                                        ControlBindOnIndexChanged.ValueMember = HiStaff.iSystem.Data.COL_ID;
                                    //DataTable data = OracleHelper.ExcuteSelectDataTable(StaticObject.PREFIX_PROC_SELECT_LIST + ControlBindOnIndexChanged.NeedDataList);
                                    DataTable data = HiStaff.iSystem.Data.DATALIST.Tables[ControlBindOnIndexChanged.NeedDataList];
                                    DataTable dSource;
                                    if (string.IsNullOrEmpty(ControlBindOnIndexChanged.NeedDataFilter))
                                    {
                                        dSource = data.Copy();
                                    }
                                    else
                                    {
                                        //DataView dv = data.DefaultView;
                                        //dv.RowFilter = string.Format(ControlBindOnIndexChanged.NeedDataFilter, this.SelectedValue);
                                        //dSource = dv.Table;

                                        dSource = data.Clone();
                                        DataRow[] dr = data.Select(string.Format(ControlBindOnIndexChanged.NeedDataFilter, this.SelectedValue));
                                        foreach (DataRow idr in dr)
                                        {
                                            dSource.ImportRow(idr);
                                        }
                                    }
                                    if (ControlBindOnIndexChanged.AddFirstNullRow)
                                        dSource.Rows.InsertAt(dSource.NewRow(), 0);
                                    ControlBindOnIndexChanged.SelectedItem = null;
                                    ControlBindOnIndexChanged.DataSource = dSource;
                                    ControlBindOnIndexChanged.SelectedIndex = 0;
                                }
                                else
                                {
                                    var chkTable = (from o in HiStaff.iSystem.Data.ALLTABLE
                                                    where o.TABLE_NAME.ToUpper() == ControlBindOnIndexChanged.NeedDataList.ToUpper()
                                                    select o).Count();
                                    if (string.IsNullOrEmpty(ControlBindOnIndexChanged.DataMember))
                                        ControlBindOnIndexChanged.DisplayMember = HiStaff.iSystem.Data.COL_NAME;
                                    if (string.IsNullOrEmpty(ControlBindOnIndexChanged.ValueMember))
                                        ControlBindOnIndexChanged.ValueMember = HiStaff.iSystem.Data.COL_ID;
                                    DataTable data = Service.ExcuteStoreToDataTable(HiStaff.iSystem.Data.PREFIX_PROC_SELECT + ControlBindOnIndexChanged.NeedDataList);
                                    DataTable dSource;
                                    if (string.IsNullOrEmpty(ControlBindOnIndexChanged.NeedDataFilter))
                                    {
                                        dSource = data;
                                    }
                                    else
                                    {
                                        //DataView dv = data.DefaultView;
                                        //dv.RowFilter = this.NeedDataFilter;
                                        //dSource = dv.Table;

                                        dSource = data.Clone();
                                        DataRow[] dr = data.Select(string.Format(ControlBindOnIndexChanged.NeedDataFilter, this.SelectedValue));
                                        foreach (DataRow idr in dr)
                                        {
                                            dSource.ImportRow(idr);
                                        }
                                    }
                                    if (ControlBindOnIndexChanged.AddFirstNullRow)
                                        dSource.Rows.InsertAt(dSource.NewRow(), 0);
                                    ControlBindOnIndexChanged.DataSource = dSource;
                                    ControlBindOnIndexChanged.SelectedIndex = 0;
                                }
                            }
                        }
                        else
                        {
                            ControlBindOnIndexChanged.SelectedItem = null;
                            ControlBindOnIndexChanged.DataSource = null;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ControlBindOnIndexChanged != null)
                    ControlBindOnIndexChanged.Text = ex.Message;
                else this.Text = ex.Message;
            }
        }
        
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return && !string.IsNullOrEmpty(this.DisplayMember))
            {
                ((DataTable)this.DataSource).DefaultView.RowFilter = string.Format("{0} LIKE '%{1}%'", this.DisplayMember, this.Text);
                this.DroppedDown = true;
            }
            base.OnKeyPress(e);
        }
        protected override void OnLostFocus(EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.DisplayMember))
                ((DataTable)this.DataSource).DefaultView.RowFilter = string.Format("{0} LIKE '%{1}%'", this.DisplayMember, string.Empty);
            base.OnLostFocus(e);
        }
    }
}
